var express = require("express");
var serveStatic = require("serve-static");
var path = require("path");
var mongoose = require("mongoose");

var cookieParser = require('cookie-parser');
var session = require('express-session');
var mongoStore = require("connect-mongo")(session);

var bodyParser = require("body-parser");
var dbUrl = "mongodb://localhost/imooc" // 数据库地址
var port = process.env.prot || 3002;

mongoose.connect(dbUrl);

var app = express();
// 视图
app.set("views", "./app/views/pages");
// 视图模板引擎
app.set("view engine", "jade");
// 表单数据解析
app.use(bodyParser.urlencoded({ extended: true }));
// 静态资源解析
app.use(serveStatic("public"));

app.locals.moment = require('moment')

app.use(cookieParser())
app.use(session({
    secret: 'imooc',
    resave: false,
    saveUninitialized: true,
    store: new mongoStore({
        url: dbUrl,
        collection: 'sessions'
    }),
}))

app.use(require('connect-multiparty')());

// 实时加载
// console.log(app.get('env'))
if ('development' === app.get('env')) {
    // 开发配置
    var logger = require('morgan')
    app.set('showStackError', true)
    app.use(logger(':method :url :status'))
    app.locals.pretty = true // 格式化代码
    mongoose.set('debug', true)

    var liveReloadPort = process.env.LR_PORT || 35279
    var excludeList = ['.woff', '.flv']

    app.use(require('connect-livereload')({
        port: liveReloadPort,
        excludeList: excludeList
    }))
}

app.listen(port);

console.log("it has started at port :" + port);


require('./config/routes')(app)


